//gcc -Wall -O3 -o bin/b b.c -I./include -L./lib -lmysqlclient
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include "mysql.h"
#include <string.h>
#include <pthread.h>
#include "my_random.h"

pthread_mutex_t mutex;
int error = 0;

int process(int i)
{
  int orderid = i;
  int max =64;
  MYSQL *mysql = NULL;
  MYSQL_ROW row;
  MYSQL_RES *res = NULL;
  char query_update[2048];
  char query_select[2048];
  char query_delete[2048];
  char query_insert[2048];
  int flag = 0, rows = 0;
  char staticinfo[] = "aaaaaaaaaaaaaaaaaaaa";
  mysql = (MYSQL *) malloc(sizeof (MYSQL));
  mysql_init(mysql);

  //if(!mysql_real_connect(mysql,"172.17.4.50","oxwf_rw","oxwangqiang","oxwf",3358,NULL,0))
  if (!mysql_real_connect(mysql, "172.17.4.48", "oxwangfeng", "oxwangqiang", "oxwf", 3333, NULL, 0))
  {
    printf("Eror connecting to database: %s,error code:%d\n", mysql_error(mysql), mysql_errno(mysql));
    return 1;
  }
  mysql->reconnect = 1;
  for ( orderid =0; orderid < 32; orderid++)
  {
    if (orderid == 0)
    {
      sprintf(query_delete, "truncate table tborderextend");
      flag = mysql_real_query(mysql, query_delete, (unsigned int) strlen(query_delete));
      if (flag)
      {
        printf("Thread id is %lu, %s, %s, %d\n", mysql_thread_id(mysql), query_delete, mysql_error(mysql), mysql_errno(mysql));
      }
    } else
    {
      sprintf(query_delete, "truncate table tborderextend%d", orderid);
      flag = mysql_real_query(mysql, query_delete, (unsigned int) strlen(query_delete));
      if (flag)
      {
        printf("Thread id is %lu, %s, %s, %d\n", mysql_thread_id(mysql), query_delete, mysql_error(mysql), mysql_errno(mysql));
      }
    }
  }
  mysql_close(mysql);
  mysql = NULL;
  return 0;
}

void *test(void *agc)
{
  process(0);
  pthread_exit(NULL);
}

int main()
{
  int i = 0;
  int tt[1024];
  pthread_mutex_init( &mutex, NULL);
  pthread_t thd[1024];
  int res, max_thread=2;
  for (i = 0; i < max_thread; i++)
  {
    res = pthread_create(&thd[i], NULL, test, NULL);
    if (res)
    {
      printf("thread create failed\n");
    }
  }

  printf("Thread num:%d\n", i);
  for ( i-- ; i > 0; i--)
  {
    pthread_join(thd[i], NULL);
  }

  printf("error is : %d\n", error);
  return 0;
}
